package com.jh.maven.hw;

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

class Infro{
    public String name;
    public String sex;
    public String phoneNumber;
    public String address;
    public String e_mail;
    public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getE_mail() {
		return e_mail;
	}
	public void setE_mail(String e_mail) {
		this.e_mail = e_mail;
	}

	static int index = 0;
	static ArrayList<Infro> list = new ArrayList<Infro>();
    static int len = list.size();
    //构造函数
    public Infro(String name,String sex,String phoneNumber,String address,String e_mail){
        this.name = name;
        this.sex = sex;
        this.phoneNumber = phoneNumber;
        this.address = address;
        this.e_mail = e_mail;
    }
    public Infro() {
		// TODO Auto-generated constructor stub
	}
	public String toString(){
        return " 姓名："+name+" 性别："+sex+" 电话："+phoneNumber+" 地址："+address+" 邮箱地址："+e_mail;
    }

    /*
                            * 添加功能
     * */
    public static void addFunction() throws IOException{//添加功能
        Infro infro = new Infro("","","","","");
        System.out.println("请输入添加的数据：");
        Scanner in = new Scanner(System.in);
        System.out.print("输入姓名：");
        infro.name = in.next();
        System.out.print("输入性别：");
        infro.sex = in.next();
        System.out.print("输入电话：");
        infro.phoneNumber = in.next();
        System.out.print("输入地址：");
        infro.address = in.next();
        System.out.print("输入邮箱：");
        infro.e_mail = in.next();
        list.add(index,infro);
        index++;
        if(list.isEmpty()){
            System.out.println("数据添加失败啦");
        }else{
            System.out.println("数据添加成功啦");
            len++;//list集合长度加一
        }
        FileWriter writer = new FileWriter("通讯录管理.txt");
        for(int i = 0 ;i<len;i++){
            String []strwriter = new String[len];
            strwriter[i]=list.get(i).toString();
            writer.write(strwriter[i]);
            writer.write("\r\n");
        }
        System.out.println("成功写入一行数据到 通讯录管理.txt 中");
        writer.close();//关闭写入流，释放资源
    }
    /*
                            * 删除功能
     * */
     public static void deleteFunction(){
         System.out.print("输入要删除的联系人的姓名:");
         Scanner in_2 = new Scanner(System.in);
         String d1 = in_2.nextLine();
         java.util.Iterator<Infro> it = list.iterator();
         while (it.hasNext()){
               Infro infro = it.next();
               if (infro.name.equals(d1)){
                    it.remove();
                    --index;
                    --len;
               }
         }
         System.out.println("成功删除一个联系人！");
         FileWriter writer = null;
 		try {
 			writer = new FileWriter("通讯录管理.txt");
 		} catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}
          for(int i = 0 ;i<len;i++){
              String []strwriter = new String[len];
              strwriter[i]=list.get(i).toString();
              try {
 				writer.write(strwriter[i]);
 			} catch (IOException e1) {
 				// TODO Auto-generated catch block
 				e1.printStackTrace();
 			}
              try {
 				writer.write("\r\n");
 			} catch (IOException e) {
 				// TODO Auto-generated catch block
 				e.printStackTrace();
 			}
          }
          try {
 			writer.close();
 		} catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();
 		}//关闭写入流，释放资源
        }
     /*
      *                     修改功能
      * */
     public static void reditFunction() throws IOException{
         System.out.print("输入要修改的通讯录的姓名：");
         Scanner in_3 = new Scanner(System.in);
         String r1 = in_3.nextLine();
         for(int a = 0; a < len;a++){
             if(r1.equals(list.get(a).name)){
                 System.out.print("输入修改后的性别：");
                 String sex_1 = in_3.next();
                 list.get(a).sex = sex_1;
                 System.out.print("输入修改后的电话：");
                 String phoneNumber_1 = in_3.next();
                 list.get(a).phoneNumber = phoneNumber_1;
                 System.out.print("输入修改后的地址：");
                 String address_1 = in_3.next();
                 list.get(a).address = address_1;
                 System.out.print("输入修改后的邮箱：");
                 String e_mail_1 = in_3.next();
                 list.get(a).e_mail = e_mail_1;
                 System.out.print("数据修改完毕");
             }
         }
		FileWriter writer = null;
		try {
			writer = new FileWriter("通讯录管理.txt");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
         for(int i = 0 ;i<len;i++){
             String []strwriter = new String[len];
             strwriter[i]=list.get(i).toString();
             try {
				writer.write(strwriter[i]);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
             try {
				writer.write("\r\n");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
         }
         try {
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//关闭写入流，释放资源
     }
        /*
                                * 查询功能
        * */
    public static void searchFunction() throws Exception{//查询功能
        System.out.print("请输入要查询的姓名：");
        Scanner in_4 = new Scanner(System.in);
        String s1=in_4.nextLine();
        for(int a= 0; a<len;a++){
            if(s1.equals(list.get(a).name)){
                System.out.println(list.get(a).toString());
            }
        }
    }

    /*
                            * 显示功能
     * */
    public static void showFunction() throws IOException{
    	System.out.println("所有联系人：");
        FileReader reader = new FileReader("通讯录管理.txt");
        BufferedReader br = new BufferedReader(reader);
        String str;
        while((str = br.readLine()) != null){//每次读取一行文本，判断是否到达文件尾
            System.out.println(str);
        }
        br.close();
    }
    
    /*
     						* 读取功能
     * */
    public static void readFunction() throws IOException{
    	 FileReader reader = new FileReader("通讯录管理.txt");
         BufferedReader br = new BufferedReader(reader);
         String lineTxt = null;
			while ((lineTxt = br.readLine()) != null)
			 {
			    Infro infro =new Infro("","","","","");
			     String[] a= lineTxt.split("[：\\s+]");
			     infro.name=a[2];
			     infro.sex=a[4];
			     infro.phoneNumber=a[6];
			     infro.address=a[8];
			     infro.e_mail=a[10];
			     list.add(index,infro);
			     index++;
			 }
		br.close();
		reader.close();
    }
}